package com.vcaresolution.logistic.controller;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.LoginForm;
import com.vcaresolution.logistic.to.UserProfileTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>LoginController</code> is used to track all login related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
@RequestMapping(value=ApplicationConstants.LOGIN_URL)
public class LoginController extends BaseController{

	
	
	private LoginForm loginForm;
	
	@Autowired
	public void setLoginForm(LoginForm loginForm) {
		this.loginForm = loginForm;
	}
	
	@ModelAttribute(value=ApplicationConstants.LOGINFORM_MODELATTRIBUTE)
	public LoginForm createForm(){
		return this.loginForm;
	}
	
	/**
	 * This method is called when the GET request has been made to the controller
	 */
	@RequestMapping(method = RequestMethod.GET)
	public void loadPage() {
	}
	
	/**
	 * This method is called when the POST request has been made to the controller
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView processRequest(@Valid LoginForm loginForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		final ModelAndView modelAndView = new ModelAndView();
		
		if( !result.hasErrors() ){
			UserProfileTO userProfile = getServiceRegistry().getUserService().login(loginForm);
			
			if( userProfile != null ){
				request.getSession().setAttribute(ApplicationConstants.USERPROFILE_SESSION_OBJECT, userProfile);
				
				/*ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
			    HttpSession session = attributes.getRequest().getSession(true);
			    UserProfileTO userProfileTO = (UserProfileTO) session.getAttribute(ApplicationConstants.USERPROFILE_SESSION_OBJECT);
			    boolean hasTokenRights = userProfileTO.hasTokenRights("canAccessApplication", "canDoJobregister");
			    
				System.out.println("hasTokenRights ::::::::::::: "+hasTokenRights);*/
				modelAndView.setViewName(ApplicationConstants.HOME_VIEW);
			}
			else{
				redirectAttrs.addFlashAttribute(ApplicationConstants.ERROR_MESSAGE, getMessageSource().getMessage("Error.loginForm.invalidlogin", null, Locale.getDefault()));
				modelAndView.setView(new RedirectView(ApplicationConstants.LOGIN_VIEW,true,true,true));
			}
		}
		
		return modelAndView;
	}
}